﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLocalChurch.Business;
using MyLocalChurch.Constants;
using MyLocalChurch.Models.Enumerations;
using RKConsulting.Helpers;
using RKConsulting.Web.Helpers;

namespace MyLocalChurch.KidsChurch
{
    public partial class EditGuardian : System.Web.UI.Page
    {
        private readonly MyContext _context = MyContextSingleton.Instance;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            Session[ImageHandler.SessionName] = null;

            ddlGender.BindEnumValues<Gender>();
            LoadGuardian();
        }

        protected void LoadGuardian()
        {
            var guardianId = QueryHelper.GetIntQueryParam(QueryParameters.GuardianId);
            var guardian = GuardianManager.GetGuardianAndUpdateCache(_context, guardianId);

            txtFirstName.Text = guardian.FirstName;
            txtMiddleName.Text = guardian.MiddleName;
            txtLastName.Text = guardian.LastName;
            txtNickName.Text = guardian.NickName;
            ddlGender.SelectByText(((Gender)guardian.Gender).ToString());
            dpBirthDate.SelectedDate = guardian.BirthDate;
            txtMobileNumber.Text = guardian.MobileNumber;
            txtEmailAddress.Text = guardian.EmailAddress;
            txtAdditionalInformation.Text = guardian.AdditionalInformation;

            if (guardian.Photo != null)
            {
                Session[ImageHandler.SessionName] = guardian.Photo.ImageData;
                var imageHandlerUrl = ConfigurationManager.AppSettings[AppSettingKeys.ImageHandlerUrl];
                imagePreview.ImageUrl = imageHandlerUrl;
            }
        }

        protected void btnPhotoPreview_Click(object sender, EventArgs e)
        {
            Session[ImageHandler.SessionName] = fileUploadPhoto.FileBytes;
            var imageHandlerUrl = ConfigurationManager.AppSettings[AppSettingKeys.ImageHandlerUrl];
            imagePreview.ImageUrl = imageHandlerUrl;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (!Page.IsValid)
                return;

            var guardianId = QueryHelper.GetIntQueryParam(QueryParameters.GuardianId);
            var guardian = GuardianManager.GetGuardianAndUpdateCache(_context, guardianId);

            guardian.FirstName = txtFirstName.Text;
            guardian.MiddleName = txtMiddleName.Text;
            guardian.LastName = txtLastName.Text;
            guardian.NickName = txtNickName.Text;
            guardian.Gender = (int)EnumHelper.Parse<Gender>(ddlGender.SelectedValue);
            guardian.BirthDate = dpBirthDate.SelectedDate;
            guardian.MobileNumber = txtMobileNumber.Text;
            guardian.EmailAddress = txtEmailAddress.Text;
            guardian.AdditionalInformation = txtAdditionalInformation.Text;

            if (guardian.Photo == null)
                guardian.Photo = PhotoManager.CreatePhotoFromFileUploadOrSession(fileUploadPhoto, HttpContext.Current);
            else
            {
                var imageData = PhotoManager.GetImageDataFromFileUploadOrSession(fileUploadPhoto, HttpContext.Current);
                
                if (imageData != null && imageData.Length != 0)
                    guardian.Photo.ImageData = imageData;

                if (fileUploadPhoto.PostedFile != null)
                {
                    guardian.Photo.Name = fileUploadPhoto.PostedFile.FileName;
                    guardian.Photo.ContentType = fileUploadPhoto.PostedFile.ContentType;
                }

                guardian.ModifiedBy = SystemConstants.User;
                guardian.ModifiedDate = DateTime.Now;
            }

            guardian.ModifiedBy = SystemConstants.User;
            guardian.ModifiedDate = DateTime.Now;

            _context.Entry(guardian).State = EntityState.Modified;
            _context.SaveChanges();

            Response.Redirect("GuardiansList.aspx");
        }
    }
}